<?php
namespace app\app\controller;

use app\common\controller\App;
use think\Db;
use think\Exception;


class House extends App
{
    protected $middleware = ['AppAuth'];

    //修改密码


    //预约看房
    public function yuyue()
    {
        $id=request()->param('id','0','intval');
        $yuyue_time=request()->param('yuyue_time');
        $yuyue_time=strtotime($yuyue_time);
        $mobile=request()->param('mobile');
        if (empty($yuyue_time)){
            return $this->fail('请填写预约时间');
        }
        if ($yuyue_time<time()){
            return $this->fail('请填写正确的预约时间');
        }


        $ismobile=preg_match("/(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{7,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}1[0-9]{10}$)/",$mobile);
        if($mobile == "" || !$ismobile){
            return $this->fail('请填写正确的手机号码');
        }

        $house=model("House")->where(['id'=>$id])->find();
        if (empty($house)){
            return $this->fail('房子不存在');
        }
        if ($house['status']==-1)
        {
            return $this->fail('房子已下架');
        }
        if ($house['status']==2){
            return $this->fail('该房子已出租');
        }
        $uid=$this->getLoginUid();
        $order=model("Order")->where(['uid'=>$uid,'house_id'=>$id])->find();
        if (!empty($order)){
            return $this->fail('您已预约过请勿重复预约');
        }

        $data['house_id']=$house['id'];
        $data['yuyue_time']=$yuyue_time;
        $data['uid']=$uid;
        $data['fangdong_id']=$house['fangdong_id'];
        $data['mobile']=$mobile;
        $data['create_time']=time();
        $res=model("Order")->insert($data);
        if ($res){
            $title='您已预约成功'.$house['title'].',预约时间'.date("Y-m-d",$yuyue_time);
            model("Message")->add_message($title,$uid);
            return $this->ok('预约成功');
        }else{
            return $this->fail('预约失败');
        }
    }

    //我的房子预约
    public function my_order()
    {
        $uid=$this->getLoginUid();
        $page=request()->param("page",'0','intval');
        $shuxing=model("House")::TAB;
        $house=model("Order")
            ->alias('a')
            ->leftJoin("House b","a.house_id=b.id")
            ->order("a.id desc")
            ->where(['a.uid'=>$uid])
            ->page($page,8)
            ->field("a.*,b.images,b.area_big,b.type,b.tab,b.title,b.price")
            ->select();
        if (!empty($house)){
            foreach ($house as $key=>$val){
                $house[$key]['image_path']=model("File")->getPath($val['images']);
                $spec=isset($shuxing[$val['type']])?$shuxing[$val['type']]:'';
                $specs=$val['area_big'].'㎡|'.$spec;
                $house[$key]['spec_title']=$specs;
                $house[$key]['sign']=model("Sign")->get_house_spec($val['tab']);
            }
        }

        $status=model("Order")::STATUS;
        $data=[
            'status'=>$status,
            'list'=>$house,
        ];
        return $this->ok('请求成功',$data);
    }

    //结束入住
    public function end_ruzhu()
    {
        $id=request()->param("id",'0','intval');
        $uid=$this->getLoginUid();
        $order=model("Order")->where(['uid'=>$uid,'id'=>$id])->find();
        if (empty($order)){
            return $this->fail('订单不存在');
        }
        if ($order['status']!=2){
            return $this->fail('您还未入住');
        }
        if ($order['is_jiaozu']==0){
            return $this->fail('请先缴纳本月房租才可退房');
        }
        $res=model('Order')->where(['uid'=>$uid,'id'=>$id])->update(['status'=>3,'end_time'=>time()]);
        if ($res){
            model("House")->where(['id'=>$res['house_id']])->update(['status'=>0]);
            return $this->ok('您已成功结束入住');
        }else{
            return $this->fail('操作失败');
        }
    }

    //缴纳房租
    public function jiaona_fangzu()
    {
        $id=request()->param("id",'0','intval');
        $password=request()->param("password");
        $beizhu=request()->param("beizhu",'');
        $uid=$this->getLoginUid();
        $order=model("Order")->where(['uid'=>$uid,'id'=>$id])->find();
        $user_info=model("Member")->where(['uid'=>$uid])->find();
        if (md5($password)!=$user_info['password']){
            return $this->fail('请填写正确的支付密码');
        }
        if (empty($order)){
            return $this->fail('订单不存在');
        }
        if ($order['status']!=2){
            return $this->fail('请先入住成功');
        }

        $res=model("Order")->where(['id'=>$id])->update(['up_pay_time'=>time(),'info_pay_money'=>0,'is_jiaozu'=>1]);
        $house_info=model("House")->where(['id'=>$order['house_id']])->find();
        if ($res){
            model("over_price")->insert(['uid'=>$uid,'house_id'=>$order['house_id'],'create_time'=>time(),'price'=>$house_info['price'],'order_id'=>$order['id'],'mobile'=>$order['mobile'],'beizhu'=>$beizhu,'fangdong_id'=>$house_info['fangdong_id']]);
            return $this->ok('缴纳成功');
        }else{
            return $this->fail('缴纳失败');
        }
    }

    //取消预约
    public function cancel_yuyue()
    {
        $id=request()->param("id",'0','intval');
        $uid=$this->getLoginUid();
        $order=model("Order")->where(['uid'=>$uid,'id'=>$id])->find();
        if (empty($order)){
            return $this->fail('订单不存在');
        }
        if (!in_array($order['status'],[0,1])){
            return $this->fail('当前状态不允许取消预约');
        }
        $res=model("Order")->where(['id'=>$order['id']])->update(['status'=>-2]);
        return $this->ok('取消预约成功');
    }

    //订单详情
    public function order_detail()
    {
        $id=request()->param("id",'0','0');
        $uid=$this->getLoginUid();
        $order=model("Order")->where(['uid'=>$uid,'id'=>$id])->find();
        if (empty($order)){
            return $this->fail('记录不存在');
        }


        $house_info=model("House")->where(['id'=>$order['house_id']])->find();
        if (!empty($house_info)){

            $house_info['images']=model("Common/File")->getPaths($house_info['images']);
            $type=model('House')::TYPE;

            $house_type=isset($type[$house_info['type']])?$type[$house_info['type']]:'';

            $address=model("Area")->area_data($house_info['city_id'],$house_info['area_id'],$house_info['address']);
            $house_info['spec_title']=$house_info['area_big'].'㎡|'.$house_type.'|'.$address;
            $house_info['tab']=Db::name("Sign")->where('id','in',$house_info['tab'])->column('title');//房子标签
            $house_config=model('HouseConfig')
                ->where('id','in',$house_info['house_config'])
                ->order("id desc")->select()->each(function ($item){
                    $item['image_path']=model("File")->getPath($item['image_id']);
                });
            $house_info['house_config']=$house_config;
            $house_info['notice']=empty($house_info['notice'])?'':$house_info['notice'];
            $fangdong=Db::name("Fangdong")->where('id','=',$house_info['fangdong_id'])->find();
            if (!empty($fangdong)){
                $fangdong['head_img']=model("File")->getPath($fangdong['head_img']);
            }
            $house_info['fangdong']=$fangdong;
        }

        $order['up_pay_time']=date("Y-m-d H:i:s",$order['up_pay_time']);


        $data=[
            'order'=>$order,
            'houes'=>$house_info,
            'status'=>model("Order")::STATUS
        ];
        return $this->ok('请求成功',$data);


    }

}
